package com.selftool.base;

import java.lang.reflect.Field;

/**
 * @program: selftool
 * @description: 抽象VO
 * @author: CYQ
 * @create: 2024-02-27 13:33
 **/
public abstract class NCVO implements INCVO{
    private Field getField(String name){
        try {
            // 获取对象的Class
            Class<?> clazz = this.getClass();
            // 使用Class的getField方法获取指定属性名的Field
            Field field = clazz.getDeclaredField(name);
            // 设置访问权限，因为getField只能访问public字段
            field.setAccessible(true);
            return field;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
    * @Description: 获取属性
    * @Param: [name]
    * @return: java.lang.Object
    * @Author: CYQ
    * @Date: 2024/2/27
    */
    public Object getAttributeValue(String name) {
        try {
            // 使用Field的get方法获取对象的属性值
            Field field = getField(name);
            Object value = field.get(this);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
    * @Description: 设置属性
    * @Param: [name, value]
    * @return: void
    * @Author: CYQ
    * @Date: 2024/2/27
    */
    public void setAttributeValue(String name,String value) {
        try {
            Field field = getField(name);
            field.set(this,value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @return
     */
    @Override
    public abstract String getTableName();

    /**
     * @return
     */
    @Override
    public String[] getAttributeNames(){
        Field[] fields = this.getClass().getDeclaredFields();
        String[] attributeNames = new String[fields.length];

        for (int i = 0; i < fields.length; i++) {
            attributeNames[i] = fields[i].getName();
        }

        return attributeNames;
    };
}
